You remove a Docker image using the docker rmi command followed by the image name, ID, or tag, but only if the image is not being used by any running or stopped containers.
The docker rmi (remove image) command deletes Docker images from your local system. An image can only be removed if it's not in use by any container—running or stopped. If an image has multiple tags, you must remove all tags or use the -f flag to force removal. Proper image management is essential for controlling disk space, as Docker images can accumulate quickly, especially during development where multiple versions are built repeatedly.
If you attempt to remove an image that is in use, Docker will display an error: Error response from daemon: conflict: unable to remove repository reference "my-app:latest" (must force) - container X is using its referenced image. This occurs even if the container is stopped. You have three options: remove the container first with docker rm, use docker rmi -f to force removal (which also removes the associated stopped container), or if the container is running, you must stop and remove it first.
The docker image prune command provides a safer cleanup alternative. Without options, it removes only dangling images (those with no tags and not referenced by any container). Adding -a removes all unused images—any image not currently referenced by a container. This is useful for clearing disk space after many builds, as unused intermediate layers and old versions accumulate. You can preview what would be removed with docker image prune --dry-run.
docker rmi my-app:latest - Remove a specific tagged image
docker rmi $(docker images -f "dangling=true" -q) - Remove all dangling images (untagged)
docker image prune -a - Remove all images not in use (aggressive cleanup)
docker system prune -a - Comprehensive cleanup: images, containers, volumes, networks
docker rmi $(docker images --filter "before=my-app:v1.0" -q) - Remove all images built before a specific version
When building images frequently in CI/CD or development, old images can consume gigabytes of disk space. Best practices include: using --rm when running containers to prevent them from lingering, regularly running docker image prune -a in development environments, and implementing retention policies to keep only recent images. For production environments, use image tagging strategies with unique identifiers (like commit SHAs) rather than overwriting tags like latest, which prevents the need to force-remove images still referenced by stopped containers.